home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / elm / elm2.4 / utils / autoreply.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-19  |  7.1 KB  |  272 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: autoreply.c,v 5.3 1993/01/20 03:37:16 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 5.3 $   $State: Exp $
  6.  *
  7.  *             Copyright (c) 1988-1992 USENET Community Trust
  8.  *             Copyright (c) 1986,1987 Dave Taylor
  9.  *******************************************************************************
  10.  * Bug reports, patches, comments, suggestions should be sent to:
  11.  *
  12.  *    Syd Weinstein, Elm Coordinator
  13.  *    elm@DSI.COM            dsinc!elm
  14.  *
  15.  *******************************************************************************
  16.  * $Log: autoreply.c,v $
  17.  * Revision 5.3  1993/01/20  03:37:16  syd
  18.  * Nits and typos in the NLS messages and corresponding default messages.
  19.  * From: dwolfe@pffft.sps.mot.com (Dave Wolfe)
  20.  *
  21.  * Revision 5.2  1992/12/11  01:45:04  syd
  22.  * remove sys/types.h include, it is now included by defs.h
  23.  * and this routine includes defs.h or indirectly includes defs.h
  24.  * From: Syd
  25.  *
  26.  * Revision 5.1  1992/10/04  00:46:45  syd
  27.  * Initial checkin as of 2.4 Release at PL0
  28.  *
  29.  *
  30.  ******************************************************************************/
  31.  
  32. /** This is the front-end for the autoreply system, and performs two 
  33.     functions: it either adds the user to the list of people using the
  34.     autoreply function (starting the daemon if no-one else) or removes
  35.     a user from the list of people.
  36.  
  37.     Usage:  autoreply filename
  38.         autoreply "off"
  39.     or  autoreply        [to find current status]
  40.     
  41. **/
  42.  
  43. #include "elmutil.h"
  44. #include "s_autoreply.h"
  45. #include <sys/stat.h>
  46.  
  47. #ifdef PWDINSYS
  48. #  include <sys/pwd.h>
  49. #else
  50. #  include <pwd.h>
  51. #endif
  52.  
  53. #define  tempdir    "/tmp/arep"        /* file prefix          */
  54. #define  autoreply_file    "/etc/autoreply.data"   /* autoreply data file  */
  55.  
  56. extern   int errno;                /* system error code    */
  57.  
  58. main(argc, argv)
  59. int    argc;
  60. char *argv[];
  61. {
  62.     char filename[SLEN];
  63.     int userid;
  64.     struct passwd *pass;
  65. #ifndef    _POSIX_SOURCE
  66.     struct passwd *getpwuid();
  67. #endif
  68.  
  69. #ifdef I_LOCALE
  70.     setlocale(LC_ALL, "");
  71. #endif
  72.  
  73.     elm_msg_cat = catopen("elm2.4", 0);
  74.  
  75.     if (argc > 2) {
  76.       printf(catgets(elm_msg_cat, AutoreplySet, AutoreplyArgsHelp1,
  77.           "Usage: %s <filename>\tto start autoreply,\n"), argv[0]);
  78.       printf(catgets(elm_msg_cat, AutoreplySet, AutoreplyArgsHelp2,
  79.           "       %s off\t\tto turn off autoreply\n"), argv[0]);
  80.       printf(catgets(elm_msg_cat, AutoreplySet, AutoreplyArgsHelp3,
  81.           "   or  %s    \t\tto check current status\n"), argv[0]);
  82.       exit(1);
  83.     }
  84.  
  85.     userid  = getuid();
  86.  
  87.     /*
  88.      * Get username (logname) field from the password entry for this user id.
  89.      */
  90.  
  91.     if((pass = getpwuid(userid)) == NULL) {
  92.       printf(catgets(elm_msg_cat, AutoreplySet, AutoreplyNoPasswdEntry,
  93.           "You have no password entry!\n"));
  94.       exit(1);
  95.     }
  96.     strcpy(username, pass->pw_name);
  97.  
  98.     if (argc == 1 || strcmp(argv[1], "off") == 0) 
  99.       remove_user((argc == 1));
  100.     else {
  101.       strcpy(filename, argv[1]);
  102.       if (access(filename,READ_ACCESS) != 0) {
  103.         printf(catgets(elm_msg_cat, AutoreplySet, AutoreplyErrRead,
  104.         "Error: Can't read file '%s'\n"), filename);
  105.         exit(1);
  106.       }
  107.       
  108.       if (filename[0] != '/') /* prefix home directory */
  109.         sprintf(filename,"%s/%s", getenv("HOME"), argv[1]);
  110.  
  111.       add_user(filename);
  112.     }
  113.  
  114.     exit(0);
  115. }
  116.  
  117. remove_user(stat_only)
  118. int stat_only;
  119. {
  120.     /** Remove the user from the list of currently active autoreply 
  121.         people.  If 'stat_only' is set, then just list the name of
  122.         the file being used to autoreply with, if any. **/
  123.  
  124.     FILE *temp, *repfile;
  125.     char  tempfile[SLEN], user[SLEN], filename[SLEN];
  126.     int   c, copied = 0, found = 0;
  127.     long  filesize, bytes();
  128.  
  129.     if (! stat_only) {
  130.       sprintf(tempfile, "%s.%06d", tempdir, getpid());
  131.  
  132.       if ((temp = fopen(tempfile, "w")) == NULL) {
  133.         printf(catgets(elm_msg_cat, AutoreplySet, AutoreplyErrOpen,
  134.         "Error: couldn't open temp file '%s'.  Not removed\n"),
  135.             tempfile);
  136.         exit(1);
  137.       }
  138.     }
  139.  
  140.     if ((repfile = fopen(autoreply_file, "r")) == NULL) {
  141.       if (stat_only) {
  142.         printf(catgets(elm_msg_cat, AutoreplySet, AutoreplyNotAutoreply,
  143.         "You're not currently autoreplying to mail.\n"));
  144.         exit(0);
  145.       }
  146.       printf(catgets(elm_msg_cat, AutoreplySet, AutoreplyNoOneAutoreply,
  147.           "No-one is autoreplying to their mail!\n"));
  148.       exit(0);
  149.     }
  150.  
  151.     /** copy out of real replyfile... **/
  152.  
  153.     while (fscanf(repfile, "%s %s %ld", user, filename, &filesize) != EOF) 
  154.  
  155.       if (strcmp(user, username) != 0) {
  156.         if (! stat_only) {
  157.           copied++;
  158.           fprintf(temp, "%s %s %ld\n", user, filename, filesize);
  159.         }
  160.       }
  161.       else {
  162.         if (stat_only) {
  163.           printf(catgets(elm_msg_cat, AutoreplySet, AutoreplyCurrArep,
  164.               "You're currently autoreplying to mail with the file %s\n"),
  165.               filename); 
  166.           exit(0);
  167.         }
  168.         found++;
  169.       }
  170.  
  171.     fclose(temp);
  172.     fclose(repfile);
  173.  
  174.     if (! found) {
  175.       if (! stat_only) {
  176.         printf(catgets(elm_msg_cat, AutoreplySet, AutoreplyNoArepOff,
  177.           "You're not currently autoreplying to mail!\n"));
  178.         unlink(tempfile);
  179.       }
  180.       else
  181.         printf(catgets(elm_msg_cat, AutoreplySet, AutoreplyNoArep,
  182.             "You're not currently autoreplying to mail.\n"));
  183.       exit(! stat_only);
  184.     }
  185.  
  186.     /** now copy tempfile back into replyfile **/
  187.  
  188.     if (copied == 0) {    /* removed the only person! */
  189.       unlink(autoreply_file);
  190.     }
  191.     else {            /* save everyone else   */
  192.       
  193.       if ((temp = fopen(tempfile,"r")) == NULL) {
  194.         printf(catgets(elm_msg_cat, AutoreplySet, AutoreplyErrReopenTemp,
  195.             "Error: couldn't reopen temp file '%s'.  Not removed.\n"),
  196.             tempfile);
  197.         unlink(tempfile);
  198.         exit(1);
  199.       }
  200.  
  201.       if ((repfile = fopen(autoreply_file, "w")) == NULL) {
  202.         printf(catgets(elm_msg_cat, AutoreplySet, AutoreplyErrReopenArep,
  203.         "Error: couldn't reopen autoreply file for writing!  Not removed.\n"));
  204.         unlink(tempfile);
  205.         exit(1);
  206.       }
  207.  
  208.       while ((c = getc(temp)) != EOF)
  209.         putc(c, repfile);
  210.  
  211.       fclose(temp);
  212.       fclose(repfile);
  213.     
  214.     }
  215.     unlink(tempfile);
  216.  
  217.     if (found > 1)
  218.       printf(catgets(elm_msg_cat, AutoreplySet, AutoreplyRemovedMulti,
  219.           "Warning: your username appeared %d times!!   Removed all\n"), 
  220.           found);
  221.     else
  222.       printf(catgets(elm_msg_cat, AutoreplySet, AutoreplyRemoved,
  223.           "You've been removed from the autoreply table.\n"));
  224. }
  225.  
  226. add_user(filename)
  227. char *filename;
  228. {
  229.     /** add the user to the autoreply file... **/
  230.  
  231.     FILE *repfile;
  232.     char  mailfile[SLEN];
  233.     long  bytes();
  234.  
  235.     if ((repfile = fopen(autoreply_file, "a")) == NULL) {
  236.       printf(catgets(elm_msg_cat, AutoreplySet, AutoreplyErrOpenArep,
  237.           "Error: couldn't open the autoreply file!  Not added\n"));
  238.       exit(1);
  239.     }
  240.     
  241.     sprintf(mailfile,"%s/%s", mailhome, username);
  242.  
  243.     fprintf(repfile,"%s %s %ld\n", username, filename, bytes(mailfile));
  244.  
  245.     fclose(repfile);
  246.  
  247.     printf(catgets(elm_msg_cat, AutoreplySet, AutoreplyAddArep,
  248.       "You've been added to the autoreply system.\n"));
  249. }
  250.  
  251.  
  252. long
  253. bytes(name)
  254. char *name;
  255. {
  256.     /** return the number of bytes in the specified file.  This
  257.         is to check to see if new mail has arrived....  **/
  258.  
  259.     int ok = 1;
  260.     extern int errno;    /* system error number! */
  261.     struct stat buffer;
  262.  
  263.     if (stat(name, &buffer) != 0)
  264.       if (errno != 2)
  265.        exit(MCprintf(catgets(elm_msg_cat, AutoreplySet, AutoreplyErrFstat,
  266.            "Error %d attempting fstat on %s\n"), errno, name));
  267.       else
  268.         ok = 0;
  269.     
  270.     return(ok ? buffer.st_size : 0L);
  271. }
  272.